package DataBase;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class Assignment {
        private int asmt_ID;
        private int ds_ID;
        private int fu_ID;
        private String asmt_time;
        private int asmt_frequency;
        private String asmt_state;
        private String asmt_path;
        private String asmt_name;
        private String asmt_ftpname;
        private String asmt_ftppwd;
        private String asmt_next;
		public int getAsmt_ID() {
			return asmt_ID;
		}
		public void setAsmt_ID(int asmtID) {
			asmt_ID = asmtID;
		}
		public int getDs_ID() {
			return ds_ID;
		}
		public void setDs_ID(int dsID) {
			ds_ID = dsID;
		}
		public int getFu_ID() {
			return fu_ID;
		}
		public void setFu_ID(int fuID) {
			fu_ID = fuID;
		}
		public String getAsmt_time() {
			return asmt_time;
		}
		public void setAsmt_time(String asmtTime) {
			asmt_time = asmtTime;
		}
		public int getAsmt_frequency() {
			return asmt_frequency;
		}
		public void setAsmt_frequency(int asmtFrequency) {
			asmt_frequency = asmtFrequency;
		}
		public String getAsmt_state() {
			return asmt_state;
		}
		public void setAsmt_state(String asmtState) {
			asmt_state = asmtState;
		}
		public String getAsmt_path() {
			return asmt_path;
		}
		public void setAsmt_path(String asmtPath) {
			asmt_path = asmtPath;
		}
		public String getAsmt_name() {
			return asmt_name;
		}
		public void setAsmt_name(String asmtName) {
			asmt_name = asmtName;
		}
		public String getAsmt_ftpname() {
			return asmt_ftpname;
		}
		public void setAsmt_ftpname(String asmtFtpname) {
			asmt_ftpname = asmtFtpname;
		}
		public String getAsmt_ftppwd() {
			return asmt_ftppwd;
		}
		public void setAsmt_ftppwd(String asmtFtppwd) {
			asmt_ftppwd = asmtFtppwd;
		}
		public String getAsmt_next() {
			return asmt_next;
		}
		public void setAsmt_next(String asmtNext) {
			asmt_next = asmtNext;
		}
		
		public void print()
		{
			System.out.println("asmt_ID  ds_ID  fu_ID  asmt_time asmt_frequency  asmt_state  " +
					           "asmt_path  asmt_name  asmt_ftpname  asmt_ftppwd  asmt_next");
		}
		public String toString(){
			
			return this.asmt_ID +"  "+ this.ds_ID +"  "+ 
			       this.fu_ID+"  "+ this.asmt_time+"  "+ 
			       this.asmt_frequency +"  "+this.asmt_state+"  "+ 
			       this.asmt_path+"  "+ this.asmt_name+"  "+
			       this.asmt_ftpname+"  "+this.asmt_ftppwd+"  "+
			       this.asmt_next+"  \n";
		}
	    public static String getTime(String hour,String minute){
	    	String time;
	    	if(hour.length() == 1)
	    		hour = "0" + hour;
	    	if(minute.length() == 1)
	    		minute = "0" + minute;
	    	time = hour + "-"+minute; 	 
	    	return time;
	    }
	    
	    
	    public static String getFullTime(String date,String time){
	    	return date + " " + time;
	    }
	    
	    public String getfulltime(){
	    	return this.asmt_next + " " + this.asmt_time;
	    }
	    

	    
	    
	    public static String getNextDate(String asmt_frequencyMain,String asmt_frequencyDetail){
	    	String date = new String();
	    	SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
	    	if(asmt_frequencyMain.equals("1")){
	    		Date today = new Date();
	    		date = df.format(today);
	    	}else if(asmt_frequencyMain.equals("2")){
	    		Calendar cal = Calendar.getInstance();
	    		int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
	    		int daycount = WeekToInt(asmt_frequencyDetail);
	    		int add = (daycount - dayofweek + 7)%7;
	    		cal.add(Calendar.DATE,add);
	    		Date next = cal.getTime();
	    		date = df.format(next);
	    	}else if(asmt_frequencyMain.equals("3")){
	    		Calendar cal = Calendar.getInstance();
	    		int todayofmonth = cal.get(Calendar.DAY_OF_MONTH);
	    		int dayofmonth = Integer.parseInt(asmt_frequencyDetail);
	    		if(todayofmonth > dayofmonth){
	    			cal.add(Calendar.MONTH,1);
	    		}
	    		cal.set(Calendar.DAY_OF_MONTH,dayofmonth);
	    		Date next = cal.getTime();
	    		date = df.format(next);
	    	}
	    	return date;
	    }

	    //提供给update使用，time 格式为hh-ss
	    public static String getNextDate(int asmt_frequencyMain,String asmt_frequencyDetail,int frequency){
	    	String date = new String();
	    	SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
	    	try {
				Date oldnextdate = df.parse(asmt_frequencyDetail);
				Calendar oldnextcal = Calendar.getInstance();
	    		oldnextcal.setTime(oldnextdate);
		    	if(asmt_frequencyMain == 1){
		    		Calendar cal = Calendar.getInstance();
//		            if(nottoday < 0){
		            	cal.add(Calendar.DATE,frequency);
//		            }
		    		Date next = cal.getTime();
		    		date = df.format(next);
		    		
		    	}else if(asmt_frequencyMain == 2){
		    		Calendar cal = Calendar.getInstance();
		    		int dayofweek = cal.get(Calendar.DAY_OF_WEEK);
		    		int daycount = oldnextcal.get(Calendar.DAY_OF_WEEK);
		    		int add = (daycount - dayofweek + 7)%7;
		    		cal.add(Calendar.DATE,add + (frequency-1)*7);
		    		if(add == 0){
		    			cal.add(Calendar.DATE,7);
		    		}
		    		Date next = cal.getTime();
		    		date = df.format(next);
		    	}else if(asmt_frequencyMain == 3){
		    		Calendar cal = Calendar.getInstance();
		    		int todayofmonth = cal.get(Calendar.DAY_OF_MONTH);
		    		int dayofmonth = oldnextcal.get(Calendar.DAY_OF_MONTH);
		    		if(todayofmonth > dayofmonth||(todayofmonth == dayofmonth)){
		    			cal.add(Calendar.MONTH,frequency);
		    		}
		    		cal.set(Calendar.DAY_OF_MONTH,dayofmonth);
		    		Date next = cal.getTime();
		    		date = df.format(next);
		    	}
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
	    	return date;
	    }	    
	    
//	    public String getFrequencyDetail(){
//	    	String asmt_frequencyDetail = null;
//	    	if(this.fu_ID == 2){
//	    		Calendar cal = Calendar.getInstance();
//	    		int dayofweek = cal.get(Calendar.DAY_OF_WEEK);	    		
//	    	}
//	    	
//	    	
//	    	return asmt_frequencyDetail;
//	    }
	    
	    
	    public void updateNext(){
//	    	String asmt_frequencyMain = this.fu_ID;
//	    	String asmt_frequencyDetail = getFrequencyDetail();
	    	String next = Assignment.getNextDate(this.fu_ID, this.asmt_next,this.asmt_frequency);
	    	this.asmt_next = next;
	    } 
	    
	    
	    public static int WeekToInt(String asmt_frequencyDetail){
	    	int dayCount = 0;
	    	if(asmt_frequencyDetail.equals("星期一")){
	    		dayCount = 2;
	    	}else if(asmt_frequencyDetail.equals("星期二")){
	    		dayCount = 3;
	    	}else if(asmt_frequencyDetail.equals("星期三")){
	    		dayCount = 4;
	    	}else if(asmt_frequencyDetail.equals("星期四")){
	    		dayCount = 5;
	    	}else if(asmt_frequencyDetail.equals("星期五")){
	    		dayCount = 6;
	    	}else if(asmt_frequencyDetail.equals("星期六")){
	    		dayCount = 7;
	    	}else if(asmt_frequencyDetail.equals("星期日")){
	    		dayCount = 1;
	    	}
	    	return dayCount;
	    }
	    
	    public static String getIP(String ip1,String ip2,String ip3,String ip4){
	    	return ip1+"."+ip2+"."+ip3+"."+ip4;
	    }
	    
	    
	
		public static void main(String[] args) {
//	    	Date date=new Date();
//	    	SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
//	    	System.out.println(df.format(date));
//	    	System.out.println();
//    		Calendar mycld = Calendar.getInstance();
//	    	mycld.setFirstDayOfWeek(4);
//    		int daycount = mycld.get(Calendar.DAY_OF_WEEK);
//    		System.out.println(daycount);
			
			
//			
//			String date = new String();
//		 	SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
//	  		Calendar cal = Calendar.getInstance();
//    		int todaycount = cal.get(Calendar.DAY_OF_WEEK);
//    		int daycount = WeekToInt("星期一");
//    		int roll = (daycount - todaycount + 7)%7;
//    		cal.add(Calendar.DATE,roll);
//    		Date next = cal.getTime();
//    		date = df.format(next);
//    		System.out.println(date);
			
//			Calendar cal = Calendar.getInstance();
//    		int dayofmonth = cal.get(Calendar.DAY_OF_MONTH);
//    		System.out.println(dayofmonth);
//	    	Date date=new Date();
//	    	SimpleDateFormat df=new SimpleDateFormat("hh-mm");
//	    	String now  = df.format(date);
//	    	System.out.println(now);
//	    	System.out.println(now.compareTo("10-02"));
			
			Assignment asmt = new Assignment();
			asmt.fu_ID = 3;
			asmt.asmt_next = "2010-11-28";
			asmt.updateNext();
			System.out.println(asmt);
	    }
}
